package com.hanxing.kxcharge.module.operations.controller.admin.costtemplateprice;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.CommonResult;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;
import static com.hanxing.kxcharge.framework.common.pojo.CommonResult.success;

import com.hanxing.kxcharge.framework.excel.core.util.ExcelUtils;

import com.hanxing.kxcharge.framework.operatelog.core.annotations.OperateLog;
import static com.hanxing.kxcharge.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.hanxing.kxcharge.module.operations.controller.admin.costtemplateprice.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.costtemplateprice.CostTemplatePriceDO;
import com.hanxing.kxcharge.module.operations.service.costtemplateprice.CostTemplatePriceService;

@Tag(name = "管理后台 - 计费模板价格时段")
@RestController
@RequestMapping("/operations/cost-template-price")
@Validated
public class CostTemplatePriceController {

    @Resource
    private CostTemplatePriceService costTemplatePriceService;

    @PostMapping("/create")
    @Operation(summary = "创建计费模板价格时段")
    @PreAuthorize("@ss.hasPermission('operations:cost-template-price:create')")
    public CommonResult<Long> createCostTemplatePrice(@Valid @RequestBody CostTemplatePriceSaveReqVO createReqVO) {
        return success(costTemplatePriceService.createCostTemplatePrice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新计费模板价格时段")
    @PreAuthorize("@ss.hasPermission('operations:cost-template-price:update')")
    public CommonResult<Boolean> updateCostTemplatePrice(@Valid @RequestBody CostTemplatePriceSaveReqVO updateReqVO) {
        costTemplatePriceService.updateCostTemplatePrice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除计费模板价格时段")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('operations:cost-template-price:delete')")
    public CommonResult<Boolean> deleteCostTemplatePrice(@RequestParam("id") Long id) {
        costTemplatePriceService.deleteCostTemplatePrice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得计费模板价格时段")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('operations:cost-template-price:query')")
    public CommonResult<CostTemplatePriceRespVO> getCostTemplatePrice(@RequestParam("id") Long id) {
        CostTemplatePriceDO costTemplatePrice = costTemplatePriceService.getCostTemplatePrice(id);
        return success(BeanUtils.toBean(costTemplatePrice, CostTemplatePriceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得计费模板价格时段分页")
    @PreAuthorize("@ss.hasPermission('operations:cost-template-price:query')")
    public CommonResult<PageResult<CostTemplatePriceRespVO>> getCostTemplatePricePage(@Valid CostTemplatePricePageReqVO pageReqVO) {
        PageResult<CostTemplatePriceDO> pageResult = costTemplatePriceService.getCostTemplatePricePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CostTemplatePriceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出计费模板价格时段 Excel")
    @PreAuthorize("@ss.hasPermission('operations:cost-template-price:export')")
    @OperateLog(type = EXPORT)
    public void exportCostTemplatePriceExcel(@Valid CostTemplatePricePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CostTemplatePriceDO> list = costTemplatePriceService.getCostTemplatePricePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "计费模板价格时段.xls", "数据", CostTemplatePriceRespVO.class,
                        BeanUtils.toBean(list, CostTemplatePriceRespVO.class));
    }

}